package com.blueseaapps.ui;

import com.blueseaapps.data.QuizOptionsData;
import com.blueseaapps.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Button;

public class QuizOptionsUI {

	private Button btnOption1;
	private Button btnOption2;
	private Button btnOption3;
	private Button btnOption4;
	private Button answer;

	private Button chosenButton;

	private AnswerSelectionListener listener;

	public QuizOptionsUI(Activity act) {
		btnOption1 = (Button) act.findViewById(R.id.btn_option1);
		btnOption2 = (Button) act.findViewById(R.id.btn_option2);
		btnOption3 = (Button) act.findViewById(R.id.btn_option3);
		btnOption4 = (Button) act.findViewById(R.id.btn_option4);

		btnOption1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				chosenButton = btnOption1;
				listener.onAnswerSelected(btnOption1.getText().toString());
			}
		});

		btnOption2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				chosenButton = btnOption2;
				listener.onAnswerSelected(btnOption2.getText().toString());
			}
		});

		btnOption3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				chosenButton = btnOption3;
				listener.onAnswerSelected(btnOption3.getText().toString());
			}
		});

		btnOption4.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				chosenButton = btnOption4;
				listener.onAnswerSelected(btnOption4.getText().toString());
			}
		});
	}

	public void setNewOptions(QuizOptionsData data){
		String[] optionArray = {data.getOption1(),data.getOption2(),data.getOption3(),data.getOption4()};
		double random = Math.random()*100;
		btnOption1.setText(data.getOption1());
		btnOption2.setText(data.getOption2());
		btnOption3.setText(data.getOption3());
		btnOption4.setText(data.getOption4());
		if(chosenButton != null){
			chosenButton.setBackgroundResource(R.drawable.btn_normal);
		}
		if(answer != null){
			answer.setBackgroundResource(R.drawable.btn_normal);
		}
	}

	public void setAnswer(String thisAnswer){
		if(thisAnswer.equalsIgnoreCase(btnOption1.getText().toString())) {
			answer = btnOption1;
		} else
		if(thisAnswer.equalsIgnoreCase(btnOption2.getText().toString())) {
			answer = btnOption2;
		} else
		if(thisAnswer.equalsIgnoreCase(btnOption3.getText().toString())) {
			answer = btnOption3;
		} else
		if(thisAnswer.equalsIgnoreCase(btnOption4.getText().toString())) {
			answer = btnOption4;
		} 
		// @Varun: Throw some kind of exception here!
	}
	
	public void correctAnswered(){
		chosenButton.setBackgroundResource(R.drawable.green);
	}

	public void incorrectAnswer(){
		chosenButton.setBackgroundResource(R.drawable.red);
		answer.setBackgroundResource(R.drawable.green);
	}


	public void setAnswerSelectedListener(AnswerSelectionListener listener){
		this.listener = listener;
	}
}
